﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HelloMVC.Model;
using HelloMVC.Interface.Repository;

namespace HelloMVC.Repository
{
    public class IssueRepository : IIssueRepository
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public IList<Issue> QueryIssues(IssueQuery query)
        {
            
            return Mapper.Instance().QueryForList<Issue>("QueryIssues", query);          
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public Int32 QueryIssuesCount(IssueQuery query)
        {
            return Mapper.Instance().QueryForObject<Int32>("QueryIssuesCount", query);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IList<Issue> QuerySubIssues(Int32 id)
        {
            return Mapper.Instance().QueryForList<Issue>("QuerySubIssues", id);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="porjectId"></param>
        /// <returns></returns>
        public IList<Issue> QueryProjectParentIssues(Int32 porjectId)
        {
            return Mapper.Instance().QueryForList<Issue>("QueryParentIssues", porjectId);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentIssue"></param>
        /// <returns></returns>
        public StepFamily CombineStepFamily(Issue parentIssue)
        {
            var subIssues = QuerySubIssues(parentIssue.Id);
            var ToDo = subIssues.Where(issue => issue.Status.Equals("T")).ToList();
            var InProgress = subIssues.Where(issue => issue.Status.Equals("I")).ToList();
            var Done = subIssues.Where(issue => issue.Status.Equals("D")).ToList();
            var stepFamily = new StepFamily { Self = parentIssue, ToDo = ToDo, InProgress = InProgress, Done = Done };

            return stepFamily;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public IList<Issue> QueryProjectOtherIssues(Int32 projectId)
        {
            return Mapper.Instance().QueryForList<Issue>("QueryOtherIssues", projectId);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public StepFamily CombineOtherIssues(Int32 projectId)
        {
            var otherIssues = QueryProjectOtherIssues(projectId);
            var ToDo = otherIssues.Where(issue => issue.Status.Equals("T")).ToList();
            var InProgress = otherIssues.Where(issue => issue.Status.Equals("I")).ToList();
            var Done = otherIssues.Where(issue => issue.Status.Equals("D")).ToList();
            var stepFamily = new StepFamily { ToDo = ToDo, InProgress = InProgress, Done = Done };
            return stepFamily;
        }
    }
}
